package cn.com.pansky.action.myaccount;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;

import net.vsame.url2sql.action.CommonAction;
import net.vsame.url2sql.helper.SqlHelper;
import net.vsame.url2sql.sql.Model;
import cn.com.pansky.action.logs.Log;
import cn.com.pansky.sdk.API;
import cn.com.pansky.service.CommonService;
import cn.com.pansky.util.CardNumUtil;

import com.alibaba.fastjson.JSONObject;

/**
 * 我的家庭成员
 * 
 * @author Administrator
 *
 */
public class FamilyMemberAction extends CommonAction {
	private Log log = new Log();
	CommonService cs = new CommonService();

	/**
	 * 根据身份证图片解析信息
	 */
	public void scanImg() throws Exception {
		
		//判断好友个数
		String sql = urlConfig.getVar("queryCount");
		Model mm = SqlHelper.queryOne(sql, user.get("AA032"));
		String num = "";
		if (null != mm) {
			num = mm.getString("SUM");
		}
		int sum = Integer.parseInt(num);
		if (sum >= 20) {
			JSONObject data = new JSONObject();
			data.put("isPass", false);
			data.put("info", "您的好友数量已达上线！");
			context.getDatas().put("result", data);
			return;
		}
		
		String imgPath = getParam("img"); // 身份证图片
		File file = new File(imgPath);// 身份证上传后的临时文件
		JSONObject jsonObject = new JSONObject();
		if (file.exists()) {
			jsonObject = cs.idendetify(imgPath);
			boolean flag = jsonObject.getBoolean("isOK");
			if (flag) {
				jsonObject.put("isPass", true);
				// 拿到身份证号，去好友列表中查询是否已是好友
				String findFriend = urlConfig.getVar("findFriend");
				String aa032 = user.getString("AA032");
				Model m = SqlHelper.queryOne(findFriend, aa032,  user.getString("AA003"), jsonObject.getString("CA006"));
				if (m != null) {
					jsonObject.put("isPass", false);
					jsonObject.put("info", "此人已经是您的好友");
				}
				String findUser1 = urlConfig.getVar("findUser1");
				Model m1 = SqlHelper.queryOne(findUser1, aa032, user.getString("AA003"));
				if (m1.getString("CA006").equals(jsonObject.getString("CA006"))) {
					jsonObject.put("isPass", false);
					jsonObject.put("info", "不能添加自己为好友");
				}
			} else {
				jsonObject.put("isPass", false);
				jsonObject.put("info", "请重新拍摄身份证照片");
			}
		} else {
			jsonObject.put("isPass", false); // 识别身份证失败，请重新拍摄照片
			jsonObject.put("info", "照片上传失败"); // 识别身份证失败，请重新拍摄照片
		}
		context.getDatas().put("result", jsonObject);
	}

	/**
	 * 根据身份证判断是否已经是好友
	 */
	public void identityFamily() throws Exception {
		
		//判断好友个数
		String sql = urlConfig.getVar("queryCount");
		Model mm = SqlHelper.queryOne(sql, user.get("AA032"));
		String num = "";
		if (null != mm) {
			num = mm.getString("SUM");
		}
		int sum = Integer.parseInt(num);
		if (sum >= 20) {
			JSONObject data = new JSONObject();
			data.put("isPass", false);
			data.put("info", "您的好友数量已达上线！");
			context.getDatas().put("result", data);
			return;
		}
		
		//String name = getParam("name1"); // 输入的名字
		String idcard = getParam("idcards"); // 输入的身份证
		idcard = CardNumUtil.cardNum(idcard);

		JSONObject jsonObject = new JSONObject();	
		jsonObject.put("isPass", true);
		jsonObject.put("info", "success");
				// 拿到身份证号，去好友列表中查询是否已是好友
				String findFriend = urlConfig.getVar("findFriend");
				String aa032 = user.getString("AA032");
				Model m = SqlHelper.queryOne(findFriend, aa032,  user.getString("AA003"), idcard);
				if (m != null) {
					jsonObject.put("isPass", false);
					jsonObject.put("info", "此人已经是您的好友");
				}
				String findUser1 = urlConfig.getVar("findUser1");
				Model m1 = SqlHelper.queryOne(findUser1, aa032, user.getString("AA003"));
				if (m1.getString("CA006").equals(idcard)) {
					jsonObject.put("isPass", false);
					jsonObject.put("info", "不能添加自己为好友");
				}			
		context.getDatas().put("result", jsonObject);
	}
	
	
	/**
	 * 保存我的家庭更成员信息
	 */
	public void saveRC03() throws Exception {
		
		String aa032 = user.getString("AA032");
		// 登录用户手机号
		Properties p = new Properties();
		p.load(getClass().getClassLoader().getResourceAsStream(
				"config.properties"));
		String basePath = p.getProperty("imgPath");
		String imgPath = basePath + "/" + user.getString("AA001") + "_img"
				+ ".JPG";
		String phoneNum = getParam("CA008"); // 好友手机号
		
//		String name = getParam("name1"); // 输入的名字
//		String idcard = getParam("idcard1"); // 输入的身份证
//		idcard = CardNumUtil.cardNum(idcard);
		
		String aa008 = "";//getParam("AA008"); // 好友关系
		File file = new File(imgPath);// 身份证上传后的临时文件
		JSONObject json =  new JSONObject();
		String AF001 = "";
		if (file.exists()) {
			JSONObject jsonObject = cs.idendetify(imgPath);
			

//        if(name.equals(jsonObject.getString("CA003"))&&idcard.equals(jsonObject.getString("CA006"))) {
        	
			// 根据手机号取得用户ID
			String sql = urlConfig.getVar("queryRC10ByAA32"); // 获取sql
			Model model = SqlHelper.queryOne(sql, aa032);
			String aa001 = model.getString("AA001"); // 用户ID
			// 根据姓名，身份证号查询自然人表
			String s = urlConfig.getVar("queryRC03ByName");
			Model m = SqlHelper.queryOne(s,	jsonObject.getString("CA006"));
			String ca001 = ""; // 服务对象id
			if (m != null) {
				// 更新自然人数据
				ca001 = m.getString("CA001"); // 取得服务对象id
				//日志记录start
				AF001 = log.start(ca001, "AF");
				
				String updRC03 = "update RC03 set CA009 = ?, CA051 = ?, CA050 = ?, CA052 = ? where CA006 = ?";
				SqlHelper.execute(updRC03, jsonObject.getString("CA009"), cs.getValueByName("AAC005",
						jsonObject.getString("CA051")),
						cs.getAac004(jsonObject.getString("CA050")), jsonObject.getString("CA052"),
						jsonObject.getString("CA006"));
			} else {
				String sql1 = urlConfig.getVar("saveRC03");// 获取sql
				// 保存自然人信息RC03
				SqlHelper.execute(sql1, jsonObject.getString("CA003"),
						jsonObject.getString("CA006"),
						jsonObject.getString("CA009"), cs.getValueByName("AAC005",
								jsonObject.getString("CA051")), cs.getAac004(jsonObject.getString("CA050")),
						jsonObject.getString("CA052"));
				// 根据姓名，身份证号获取服务对象ID
				ca001 = SqlHelper.queryOne(s, jsonObject.getString("CA006")).getString("CA001");
				//日志记录start
				AF001 = log.start(ca001, "AF");
			}
			// 根据用户ID，服务对象ID判断该好友是否已经存在，如果存在则不保存，否则保存好友
			String s1 = urlConfig.getVar("queryRA12ByAA001");
			Model ra12Model = SqlHelper.queryOne(s1, aa001, ca001);
			// 如果好友不存在，进行保存
			if (ra12Model == null) {
				// 保存我的好友表RA12
				String saveSql = urlConfig.getVar("saveRA12");
				SqlHelper.execute(saveSql, aa001, ca001, aa008, phoneNum,
						new SimpleDateFormat("yyyy-MM-dd").format(new Date()));

				String findRC05 = urlConfig.getVar("findRC05");
				Model m2 = SqlHelper.queryOne(findRC05, ca001);
				if (null != m2) {

				} else {
					// 保存证件信息表
					cs.isnertRC05(ca001, imgPath, "");
				}
			}
			json.put("isPass", true);
			json.put("info", "联系人添加成功");
			file.delete(); // 删除临时文件
//		}  else {
//			json.put("isPass", false);
//			json.put("info", "姓名或身份证号码不匹配");
//		}
	}
		else {
			json.put("isPass", false); // 保存失败
			json.put("info", "身份证照片不存在");
		}
		
		//日志记录end
		boolean result = log.end(AF001, json.get("isPass"), json.get("info"));
		System.out.println("日志：添加联系人"+result);
		
		context.getDatas().put("result", json);
	}

	/**
	 * 删除家庭成员，单条删除
	 */
	public void delRC03ById() {
		String CA001 = getParam("CA001"); // 取得服务对象ID
		//日志记录start
		String AF001 = log.start(CA001, "DF");
		
		String CA006 = user.getString("CA006");
		String sql = "select CA001 from RC03 where CA006 = ?";
		String CA001_ = SqlHelper.queryOne(sql, CA006).getString("CA001");
		if (CA001.equals(CA001_)) {
			datas.put("isPass", false);
			datas.put("info", "警告:不可删除本人！");
			context.getDatas().put("result", datas);
			//日志记录end
			boolean result = log.end(AF001, datas.get("isPass"), datas.get("info"));
			System.out.println("日志:删除联系人"+result);
			return;
		}
		String AA001 = user.getString("AA001");
		String delRA12ById = urlConfig.getVar("delRA12ById");
		int i = SqlHelper.execute(delRA12ById, AA001,CA001);
		if (i == 1) {
			datas.put("isPass", true);
			datas.put("info", "删除成功");
		} else {
			datas.put("isPass", false);
			datas.put("info", "删除失败");
		}
		
		//日志记录end
		boolean result = log.end(AF001, datas.get("isPass"), datas.get("info"));
		System.out.println("日志:删除联系人"+result);
		
		context.getDatas().put("result", datas);
	}

	public void queryRC03() {
		String queryRC03 = urlConfig.getVar("queryRC03");
		Model model = SqlHelper.queryOne(queryRC03, getParam("CA001"));
		if (null != model) {
			datas.put("isPass", true);
			datas.put("CA003", model.getString("CA003"));
			datas.put("CA006", model.getString("CA006"));
			datas.put("CA051", model.getString("CA051"));
			datas.put("CA050", model.getString("CA050"));
			datas.put("CA009", model.getString("CA009"));
		}
		context.getDatas().put("result", datas);
	}

	/**
	 * 更新好友信息
	 */
	public void updateInfo() {
		
		String CA001 = getParam("CA001");
		//日志记录start
		String AF001 = log.start(CA001, "UI");
		
		String AAE005 = context.getParam("AAE005");
		String AAE006 = context.getParam("AAE006");
		String insured = getParam("insured");
		if((null == AAE005 || "".equals(AAE005)) && (null == AAE006 || "".equals(AAE006))){
			datas.put("isPass", false);
			datas.put("info", "居住地址和联系电话不可都为空！");
			//日志记录end
			boolean result = log.end(AF001, datas.get("isPass"), datas.get("info"));
			System.out.println("日志：修改信息"+result);
			
			context.getDatas().put("result", datas);
			return;
		}

		if (insured.equals("1")) {// 参保用户

			JSONObject params = new JSONObject();
			params.put("AAC002", getCA006ByCA001());
			params.put("AAE005", AAE005);
			params.put("AAE006", AAE006);
			params.put("AAA121", "A20003");
			String userId = user.getString("AA001");
			params.put("AAE011", userId);
			JSONObject json = API.sendController("modifyController.json",
					params);

			boolean success = json.getBoolean("SUCCESS");
			if (success) {
				String aa032 = user.getString("AA032");
				String queryRC10 = urlConfig.getVar("queryRC10");
				Model m = SqlHelper.queryOne(queryRC10, CA001, aa032);
				if (null != m) {// 是用户
					String updateRC10 = urlConfig.getVar("updateRC10");
					SqlHelper.execute(updateRC10, AAE005, CA001);
				} else {// 是好友
					String updateRA12 = urlConfig.getVar("updateRA12");
					SqlHelper.execute(updateRA12, AAE005, CA001, aa032);
				}
				datas.put("isPass", true);
				datas.put("info", "修改信息申请已提交，请等待工作人员复核！");
			} else {
				datas.put("isPass", false);
				datas.put("info", json.getString("MSG"));
			}
		} else if(insured.equals("2")) {// 未参保用户
			datas.put("isPass", false);
			datas.put("info", "您的参保登记正在审核中，请您在审核完成后再修改信息！");
		} else {
			String aa032 = user.getString("AA032");
			String queryRC10 = urlConfig.getVar("queryRC10");
			Model m = SqlHelper.queryOne(queryRC10, CA001, aa032);
			if (null != m) {// 是用户
				String updateRC10 = urlConfig.getVar("updateRC10");
				SqlHelper.execute(updateRC10, AAE005, CA001);
			} else {// 是好友
				String updateRA12 = urlConfig.getVar("updateRA12");
				SqlHelper.execute(updateRA12, AAE005, CA001, aa032);
			}
			datas.put("isPass", true);
			datas.put("info", "修改成功");
		}
		//日志记录end
		boolean result = log.end(AF001, datas.get("isPass"), datas.get("info"));
		System.out.println("日志：修改信息"+result);

		context.getDatas().put("result", datas);
	}
}
